﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace YunHao
{
    /// <summary>
    /// 一些通用方法
    /// </summary>
    public class Common
    {
        #region 通用携程

        /// <summary>
        /// 在<paramref name="duration"/>后执行<paramref name="something"/>,分组在<paramref name="group"/>
        /// </summary>
        /// <param name="duration"></param>
        /// <param name="something"></param>
        /// <param name="group"></param>
        public static void DoAfter(float duration, Action something, string group = null)
        {
            CoroutineManager.Instance.CreateCoroutine(IDoAfter(duration, something), group);
        }

        /// <summary>
        /// 在<paramref name="duration"/>后执行<paramref name="something"/>,
        /// </summary>
        /// <param name="duration"></param>
        /// <param name="something"></param>
        private static IEnumerator IDoAfter(float duration, Action something)
        {
            yield return new WaitForSeconds(duration);
        }

        #endregion

        #region 多次数连续调用

        /// <summary>
        /// 连续多次调用
        /// </summary>
        /// <param name="time">调用次数</param>
        /// <param name="duration">持续时间</param>
        /// <param name="something">调用事件</param>
        /// <param name="onFinish">结束事件</param>
        /// <param name="group">分组</param>
        public static void DoRepeat(int time, float duration, Action<int> something, Action onFinish, string group = null)
        {
            CoroutineManager.Instance.CreateCoroutine(IDoRepeat(time, duration, something, onFinish), group);
        }

        /// <summary>
        /// 多次调用
        /// </summary>
        /// <param name="time">调用次数</param>
        /// <param name="duration">持续时间</param>
        /// <param name="something">调用事件</param>
        /// <param name="onFinish">结束事件</param>
        /// <returns></returns>
        private static IEnumerator IDoRepeat(int time, float duration, Action<int> something, Action onFinish)
        {
            float onceTime = duration / time;
            for (int i = 0; i < time; i++)
            {
                yield return new WaitForSeconds(onceTime);
                if (null != something) something(i);
            }
            if (null != onFinish) onFinish();
        }

        #endregion

        #region 2D 淡入淡出

        /// <summary>
        /// 2D淡入
        /// </summary>
        /// <param name="transform"></param>
        /// <param name="time"></param>
        /// <param name="onFinish"></param>
        public static void FadeIn2D(Transform transform, float time, Action onFinish = null)
        {
            int count = 20;
            float unitAlpha = 1f / count;
            DoRepeat(count, time, (index) =>
            {
                float alpha = (index + 1) * unitAlpha;
                FadeChildren(transform, alpha);
            }, onFinish);
        }

        /// <summary>
        /// 2D淡出
        /// </summary>
        /// <param name="transform"></param>
        /// <param name="time"></param>
        /// <param name="onFinish"></param>
        public static void FadeOut2D(Transform transform, float time, Action onFinish = null)
        {
            int count = 20;
            float unitAlpha = 1f / count;
            DoRepeat(count, time, (index) =>
            {
                float alpha = (count - 1 - index) * unitAlpha;
                FadeChildren(transform, alpha);
            }, onFinish);
        }

        /// <summary>
        /// 更改组件alpha
        /// </summary>
        /// <param name="transform"></param>
        /// <returns></returns>
        private static void FadeChildren(Transform transform, float alpha)
        {
            Graphic graphic = transform.GetComponent<Graphic>();
            if (graphic)
            {
                Color c = graphic.color;
                c.a = alpha;
                graphic.color = c;
            }
            SpriteRenderer spriteRenderer = transform.GetComponent<SpriteRenderer>();
            if (spriteRenderer)
            {
                Color c = spriteRenderer.color;
                c.a = alpha;
                spriteRenderer.color = c;
            }
            foreach (Transform transf in transform)
            {
                FadeChildren(transf, alpha);
            }
        }

        #endregion

        #region 数组排序
        /// <summary>
        /// 乱序排列一个数组
        /// </summary>
        public static T[] DisorderArray<T>(T[] arrays)
        {
            int min = 1;
            int max = arrays.Length;
            int inx = 0;
            T b = default(T);
            System.Random rnd = new System.Random();
            while (min != max)
            {
                int r = rnd.Next(min++, max);
                b = arrays[inx];
                arrays[inx] = arrays[r];
                arrays[r] = b;
                inx++;
            }
            return arrays;
        }
        #endregion
    }
}